package com.hbzd.protection.security;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 权限拦截,URI权限认证
 * 
 * @note 方法权限拦截
 * 
 */
public class PrivilegeInterceptor extends HandlerInterceptorAdapter {

	private static Logger logger = Logger.getLogger(PrivilegeInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
			HandlerMethod hm = (HandlerMethod) handler;
			RequestMapping requestMapping1 = hm.getBeanType().getAnnotation(RequestMapping.class);
			RequestMapping requestMapping = hm.getMethodAnnotation(RequestMapping.class);
			String[] path = requestMapping1.value();
			String[] path2 = requestMapping.value();
			String pathInfo = request.getPathInfo();
			String contextPath = request.getContextPath();
			logger.debug("PrivilegeInterceptor: " + Arrays.toString(path)
					+ "\t" + Arrays.toString(path2) + "\t" + pathInfo + "\t"
					+ contextPath + "\tAccept=" + request.getHeader("accept"));
		}
		return true;
	}
	
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}
